﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Media;

namespace Kindohm.KSynth.UI
{
    public class WaveFormPoint
    {
        Point point;
        Dictionary<WaveFormPoint, PointConnection> lineRegistry;


        public WaveFormPoint()
        {
            this.lineRegistry = new Dictionary<WaveFormPoint, PointConnection>();
        }


        public Point Point
        {
            get { return this.point; }
            set
            {
                this.point = value;
                this.Ellipse.SetValue(Canvas.LeftProperty, this.point.X);
                this.Ellipse.SetValue(Canvas.TopProperty, this.point.Y);
                this.MoveConnections();
            }
        }

        public Canvas Canvas { get; set; }
        public double MinX { get; set; }
        public double MaxX { get; set; }
        public double MinY { get; set; }
        public double MaxY { get; set; }
        public bool Movable { get; set; }
        public Ellipse Ellipse { get; set; }

        public void RegisterConnection(WaveFormPoint peer)
        {
            if (!this.lineRegistry.ContainsKey(peer)
                & !peer.lineRegistry.ContainsKey(this))
            {
                Line line = new Line();
                line.StrokeThickness = 2;
                line.Stroke = new SolidColorBrush(Colors.Red);

                line.X1 = this.Point.X;
                line.Y1 = this.Point.Y;
                line.X2 = peer.Point.X;
                line.Y2 = peer.Point.Y;

                this.Canvas.Children.Insert(0, line);

                PointConnection primaryConnection = new PointConnection();
                primaryConnection.Line = line;
                primaryConnection.IsPrimary = true;

                PointConnection secondaryConnection = new PointConnection();
                secondaryConnection.Line = line;
                secondaryConnection.IsPrimary = false;

                this.lineRegistry.Add(peer, primaryConnection);
                peer.lineRegistry.Add(this, secondaryConnection);
            }
        }

        public void UnregisterConnection(WaveFormPoint peer)
        {
            if (this.lineRegistry.ContainsKey(peer))
            {
                PointConnection connection = this.lineRegistry[peer];
                Line line = connection.Line;
                if (this.Canvas.Children.Contains(line))
                {
                    this.Canvas.Children.Remove(line);
                }

                this.lineRegistry.Remove(peer);
                peer.UnregisterConnection(this);
            }
        }

        public void MoveConnections()
        {

            foreach (WaveFormPoint key in this.lineRegistry.Keys)
            {
                PointConnection connection = this.lineRegistry[key];
                Point newPosition = this.Point;
                Line line = connection.Line;

                if (connection.IsPrimary)
                {
                    line.X1 = newPosition.X;
                    line.Y1 = newPosition.Y;
                }
                else
                {
                    line.X2 = newPosition.X;
                    line.Y2 = newPosition.Y;
                }
            }
        }



    }
}

